using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using QTool.BEPUphysics;
using QTool.BEPUphysics.Entities.Prefabs;
using QTool.BEPUphysics.Entities;
using QTool.BEPUphysics.BroadPhaseEntries.MobileCollidables;
using QTool.BEPUphysics.BroadPhaseEntries;
using QTool.BEPUphysics.NarrowPhaseSystems.Pairs;
using QTool.NetFrame;
using QTool.BEPUphysics.Materials;
using QTool.BEPUphysics.CollisionRuleManagement;
using QTool.NetFrame;
using System.Linq;
using QTool.BEPUphysics.CollisionShapes;
using QTool.BEPUphysics.CollisionShapes.ConvexShapes;
using QTool.BEPUphysics.BroadPhaseSystems;

namespace QTool.NetFrame
{
    public class FixRigidbody : QFixBehaviour
    {
        [QName("质量")]
        public Fix64 mass;
        [QName("静摩擦")]
        public Fix64 staticFriction = 1;
        [QName("动摩擦")]
        public Fix64 kineticFriction = 1;
        [QName("弹力")]
        public Fix64 bounciness = 0;
       // public bool freezeRoatation;
       // public CollisionRule collisionRule = CollisionRule.Defer;
     //   FixQuaternion lastRotation;
        public override void OnFixAwake()
        {
            base.OnFixAwake();
            List<CompoundChildData> list = new List<CompoundChildData>();

            foreach (var c in GetComponentsInChildren<FixCollider>())
            {
                var child = new CompoundChildData
                {

                    Entry = new CompoundShapeEntry(c.Shape, new RigidTransform(c.transform.position, c.transform.rotation)),
                    Tag = c,
                    CollisionRules=new CollisionRules {personal=c.Rule },

                };
                child.Tag = c;


                list.Add(child);
            }
            if (list.Count > 0)
            {
                var lastEntity = NetEntity.fixEntity;
                NetEntity.fixEntity = new CompoundBody(list, Position);
                NetEntity.spaceObject = NetEntity.fixEntity;
                if (NetEntity is IBroadPhaseEntryOwner entry)
                {
                   entry.Entry.Tag = this;
                }
                NetEntity.fixEntity.CollisionInformation.CollisionRules.Personal = CollisionRule.Defer;
                NetEntity.fixEntity.Mass = mass;
                NetEntity.fixEntity.Material.StaticFriction = staticFriction;
                NetEntity.fixEntity.Material.KineticFriction = kineticFriction;
                NetEntity.fixEntity.Material.Bounciness = bounciness;
                NetEntity.fixEntity.Tag = this;
                NetEntity.fixEntity.Position = lastEntity. Position;
                NetEntity.fixEntity.Rotation = lastEntity. Rotation;
           //     UpdateLastInfo();
             //   FixPhysics.OnBeforeUpdate += UpdateLastInfo;
             //   FixPhysics.OnAfterUpdate += FreezeInfo;
            }

          

        }
       
        public override void OnFixUpdate()
        {
        }
        public override void OnFixDestroy()
        {
            base.OnFixDestroy();
        }
     
     

    
        //  public ConvexShape shape;
   
  
        //private void OnDrawGizmosSelected()
        //{
        //    Gizmos.color = Color.black;
        //    Gizmos.DrawSphere(Vector3.one, 0.1f);
        //}
    }
    public static class FixPhysics
    {
        public static QTool.BEPUphysics.Space space = new QTool.BEPUphysics.Space();
        public static int Index { private set; get; } = 0;
        static FixPhysics()
        {
            space.ForceUpdater.gravity = Physics.gravity;
        }
        public static event System.Action OnBeforeUpdate;
        public static event System.Action OnAfterUpdate;
        public static void Update()
        {
            OnBeforeUpdate?.Invoke();
            space.Update();
            OnAfterUpdate?.Invoke();
            Index++;
        }
    }
    public interface IFixCollisionInfo
    {
        public void CollistionStart(FixCollider collier, FixCollider other);
        public void CollistionStop(FixCollider collier, FixCollider other);
    }

}
